package com.yf1706.service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yf1706.bean.BaseSalary;
import com.yf1706.bean.BaseSalarySob;
import com.yf1706.bean.Emp;
import com.yf1706.dao.jpa.IBaseSalaryJDAO;
import com.yf1706.dao.jpa.IBaseSalarySobJDAO;
import com.yf1706.dao.jpa.IEmpJDAO;
import com.yf1706.vo.BaseSalaryVO;

/**
 * 本薪服务
 * @author 锦涛
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class BaseSalaryService {
	
	@Autowired
	private IBaseSalarySobJDAO bSobJDAO;
	
	@Autowired
	private IBaseSalaryJDAO bJDAO;
	
	@Autowired
	private IEmpJDAO empJDAO;
	
	/**
	 * 根据ID查找员工
	 */
	public Emp findEmpByID(int id) {
		return empJDAO.findByEmpId(id);
	}
	
	//--------------------本薪账套-------------------------
	/**
	 * 添加本薪账套
	 */
	@Transactional
	public void saveSob(BaseSalarySob baseSalarySob) {
		bSobJDAO.save(baseSalarySob);
	}
	
	/**
	 * 删除本薪账套
	 */
	@Transactional
	public void removeSob(String ids) {
		for(String id : ids.split(",") ) {
			if ( id != null) {
				bSobJDAO.deleteById(Integer.parseInt(id));
			}
		}
	}
	
	/**
	 * 修改本薪账套
	 */
	@Transactional
	public void updateSob(BaseSalarySob baseSalarySob) {
		Optional<BaseSalarySob> SbaseSalarySob = bSobJDAO.findById(baseSalarySob.getBaseSalarySobId());
		SbaseSalarySob.get().setBaseSalarySobName(baseSalarySob.getBaseSalarySobName());
		SbaseSalarySob.get().setEmpNum(baseSalarySob.getEmpNum());
		SbaseSalarySob.get().setRemark(baseSalarySob.getRemark());
	}
	
	/**
	 * 分页查询本薪账套
	 */
	@Transactional
	public Page<BaseSalarySob> querySobByPage(int page,int rows,String sort,String order){
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return bSobJDAO.findAll(pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return bSobJDAO.findAll(pageable);
		}
	}
	
	/**
	 * 模糊分页查询本薪账套
	 */
	@Transactional
	public Page<BaseSalarySob> findSearch(String baseSalarySobName,int page,int rows,String sort,String order) {
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return bSobJDAO.findSearch(baseSalarySobName, pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return bSobJDAO.findSearch(baseSalarySobName, pageable);
		}
	}
	
	/**
	 * 查询所有的本薪账套
	 */
	public List<BaseSalarySob> querySobAll(){
		return bSobJDAO.findAll();
	}
	
	/**
	 * 通过ID查询本薪账套
	 */
	public BaseSalarySob findSobByID(int id) {
		return bSobJDAO.getOne(id);
	}
	
	//--------------------本薪表-------------------------
	/**
	 * 添加本薪
	 */
	@Transactional
	public void saveBS(BaseSalary baseSalary) {
		bJDAO.save(baseSalary);
		Optional<BaseSalarySob> SbaseSalarySob = bSobJDAO.findById(baseSalary.getBaseSalarySob().getBaseSalarySobId());
		SbaseSalarySob.get().setEmpNum(SbaseSalarySob.get().getEmpNum()+1);
	}
	
	/**
	 * 删除本薪
	 */
	@Transactional
	public void removeBS(String ids) {
		for(String id : ids.split(",") ) {
			if ( id != null) {
				bJDAO.deleteById(Integer.parseInt(id));
				Optional<BaseSalarySob> SbaseSalarySob = bSobJDAO.findById(bJDAO.getOne(Integer.parseInt(id)).getBaseSalarySob().getBaseSalarySobId());
				SbaseSalarySob.get().setEmpNum(SbaseSalarySob.get().getEmpNum()-1);
			}
		}
	}
	
	/**
	 * 修改本薪
	 */
	@Transactional
	public void updateBS(BaseSalary baseSalary) {
		Optional<BaseSalary> SBaseSalary = bJDAO.findById(baseSalary.getBaseSalaryId());
		SBaseSalary.get().setBaseSalaryValue(baseSalary.getBaseSalaryValue());
		SBaseSalary.get().setSalaryBeginDate(baseSalary.getSalaryBeginDate());
		SBaseSalary.get().setSalaryOffDate(baseSalary.getSalaryOffDate());
		SBaseSalary.get().setMender(baseSalary.getMender());
		SBaseSalary.get().setMendDate(baseSalary.getMendDate());
		SBaseSalary.get().setApprover(baseSalary.getApprover());
		SBaseSalary.get().setApproveDate(baseSalary.getApproveDate());
		SBaseSalary.get().setRemark(baseSalary.getRemark());
	}
	
	/**
	 * 分页查询本薪
	 */
	@Transactional
	public Page<BaseSalaryVO> queryBSByPage(int id,int page,int rows,String sort,String order){
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return bJDAO.findSearch(id, pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return bJDAO.findSearch(id, pageable);
		}
	}
	
	/**
	 * 查询所有本薪表
	 */
	public List<BaseSalary> queryBSAll(){
		return bJDAO.findAll();
	}
	
}
